package dev.xesam.androidkit.utils;

import java.security.Provider;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.Application;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.ProviderInfo;
import android.content.pm.ServiceInfo;
import android.os.Build;
import android.os.Bundle;

/**
 * Created by xe on 15-3-30.
 */
public final class MetaUtil {

    @SuppressWarnings("unchecked")
	private static <T> T get(Bundle bundle, String name) {
        Object object = bundle.get(name);
        if (object == null) {
            return null;
        } else {
            return (T) object;
        }
    }

    public static <T> T getFromApplication(Application application, String name) {
        try {
            ApplicationInfo info = application.getPackageManager().getApplicationInfo(application.getPackageName(), PackageManager.GET_META_DATA);
            return get(info.metaData, name);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static <T> T getFromActivity(Activity activity, String name) {
        try {
            ActivityInfo info = activity.getPackageManager().getActivityInfo(activity.getComponentName(), PackageManager.GET_META_DATA);
            return get(info.metaData, name);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static <T> T getFromService(Service service, String name) {
        try {
            ServiceInfo info = service.getPackageManager().getServiceInfo(new ComponentName(service, service.getClass()), PackageManager.GET_META_DATA);
            return get(info.metaData, name);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static <T> T getFromBroadcast(Context context, BroadcastReceiver broadcastReceiver, String name) {
        try {
            ActivityInfo info = context.getPackageManager().getReceiverInfo(new ComponentName(context, broadcastReceiver.getClass()), PackageManager.GET_META_DATA);
            return get(info.metaData, name);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            return null;
        }
    }

    @TargetApi(Build.VERSION_CODES.GINGERBREAD) public static <T> T getFromProvider(Context context, Provider provider, String name) {
        try {
            ProviderInfo info = context.getPackageManager().getProviderInfo(new ComponentName(context, provider.getClass()), PackageManager.GET_META_DATA);
            return get(info.metaData, name);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            return null;
        }
    }
}
